using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Microblaze.Simulator.Engine;

namespace Weazel.Microblaze.Simulator.Components.Microblaze.Instructions
{
    public class Bgt
      : ConditionalBranchInstruction
    {
        private enum BgtInstructionKind
        {
            None,   // error
            bgt,    // branch if greater
            bgtd    // branch if greater with delay
        }

        private BgtInstructionKind kind;

        public override int CycleCount
        {
            get
            {
                if (BranchStatus == BranchInstructionCycleCountStatus.BranchTaken)
                    return 1;
                else
                    return kind == BgtInstructionKind.bgtd ? 2 : 3;
            }
        }

        public Bgt()
            : base("bgt")
        {
        }

        public override void Set(uint address, uint value)
        {
            base.Set(address, value);

            kind =
              DestinationRegister == 0x14 ? BgtInstructionKind.bgtd :
              DestinationRegister == 0x04 ? BgtInstructionKind.bgt :
              BgtInstructionKind.None;

            if (kind == BgtInstructionKind.None)
                throw new UnknownInstructionException(value);
        }

        /// <summary>
        /// Branch if rA is greater than 0, to the instruction located in the 
        /// offset value of rB. The target of the branch will be the instruction 
        /// at address PC + rB. The mnemonic bgtd will set theD bit. The D bit 
        /// determines whether there is a branch delay slot or not. If the D bit 
        /// is set, it means that there is a delay slot and the instruction fol-
        /// lowing the branch (i.e. in the branch delay slot) is allowed to 
        /// complete execution before executing the target instruction. If the D 
        /// bit is not set, it means that there is no delay slot, so the 
        /// instruction to be executed after the branch is the target instruction.
        /// </summary>    
        public override void Execute(MicroblazeComponent MicroblazeComponent)
        {
            // note use of int for comparison
            int sa = (int)MicroblazeComponent.ReadRegister(SourceRegisterA);

            if (sa > 0)
            {
                MicroblazeComponent.Pc = MicroblazeComponent.Pc + MicroblazeComponent.ReadRegister(SourceRegisterB);
                BranchStatus = BranchInstructionCycleCountStatus.BranchTaken;

                if (kind == BgtInstructionKind.bgtd)
                    MicroblazeComponent.DelayedBranchActive = true;
            }
            else
            {
                MicroblazeComponent.Pc += 4;
                BranchStatus = BranchInstructionCycleCountStatus.BranchNotTaken;
            }
        }

        public override string ToString()
        {
            return string.Format("{0} r{1}, r{2}",
              kind,
              SourceRegisterA,
              SourceRegisterB
            );
        }
    }
}
